package com.petalsgroup.petals.service.animation.impl;

import com.petalsgroup.petals.dao.Comment.CommentDao;
import com.petalsgroup.petals.dao.Comment.Impl.CommentDaoImpl;
import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.pojo.CommentsVO;
import com.petalsgroup.petals.pojo.PageBean;
import com.petalsgroup.petals.service.animation.CommentService;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CommentServiceImpl implements CommentService {
    private final CommentDao commentDao=new CommentDaoImpl();
    @Override
    public List<CommentsDO> getAllComments(Integer aid) {
        return commentDao.getAllCommentsList(aid);
    }

    @Override
    public CommentsDO getMyComment(Integer aid, Integer uid) {
        return commentDao.getMyComment(aid,uid);
    }

    @Override
    public List<String> getComment(List<CommentsDO> commentsList) {
        List<Integer> userIDList=new ArrayList<>();
        for (CommentsDO cDo:commentsList) {
            userIDList.add(cDo.getUid());
        }
        List<String> userNameList=new ArrayList<>();
        for (Integer uid:userIDList) {
            userNameList.add(commentDao.getAUserName(uid));
        }
        return userNameList;
    }

    @Override
    public String getMyUserName(Integer uid) {
        return commentDao.getAUserName(uid);
    }

    @Override
    public void addComment(int aid, int uid, String comment_content, Timestamp timestamp, Integer score) {
        Integer userComment= commentDao.getAUserComment(aid,uid);
        //System.out.println(userComment);
        if(userComment==0){
            commentDao.addUserComment(aid,uid,comment_content,score,timestamp);
            commentDao.updateAnimationPeopleNum(aid);
            commentDao.updateAnimationScore(aid);
        }else {
            commentDao.setUserComment(aid,uid,comment_content,score,timestamp);
            commentDao.updateAnimationScore(aid);
        }
    }

    @Override
    public PageBean<CommentsVO> pageQuery(int aid, int currentPage, int num, int uid) {
        //封装PageBean
        PageBean<CommentsVO> pb=new PageBean<>();
        //设置当前页码
        pb.setCurrentPage(currentPage);
        //设置每页显示条数
        pb.setRows(num);
        //查询总记录数
        int totalCount=commentDao.findTotalCount(aid);
        //设置总记录数
        pb.setTotalCount(totalCount);
        //根据aid，start，num查询当前页的数据集合
        int start=(currentPage-1)*num; //开始的记录数
        List<CommentsDO> listDO=commentDao.findByPage(aid,start,num);
        List<CommentsVO> listVO=new ArrayList<>();
        for (CommentsDO commentsDO:listDO) {
            CommentsVO commentsVO = new CommentsVO();
            commentsVO.setCommentsDO(commentsDO);
            if(commentDao.findByUid(uid,commentsDO.getCom_id())>0){
                commentsVO.setFlag(false);
            }else commentsVO.setFlag(true);
            listVO.add(commentsVO);
        }
        listVO.forEach(System.out::println);
        //设置当前页显示的数据集合
        pb.setList(listVO);
        //设置总页数
        int totalPage=totalCount%num==0?(totalCount/num):(totalCount/num)+1;
        pb.setTotalPage(totalPage);
        return pb;
    }
    @Override
    public PageBean<CommentsVO> pageTimeQuery(int aid, int currentPage, int num, Integer uid) {
        //封装PageBean
        PageBean<CommentsVO> pb=new PageBean<>();
        //设置当前页码
        pb.setCurrentPage(currentPage);
        //设置每页显示条数
        pb.setRows(num);
        //查询总记录数
        int totalCount=commentDao.findTotalCount(aid);
        //设置总记录数
        pb.setTotalCount(totalCount);
        //根据aid，start，num查询当前页的数据集合
        int start=(currentPage-1)*num; //开始的记录数
        List<CommentsDO> listDO=commentDao.findByTimePage(aid,start,num);
        List<CommentsVO> listVO=new ArrayList<>();
        for (CommentsDO commentsDO:listDO) {
            CommentsVO commentsVO = new CommentsVO();
            commentsVO.setCommentsDO(commentsDO);
            if(commentDao.findByUid(uid,commentsDO.getCom_id())>0){
                commentsVO.setFlag(false);
            }else commentsVO.setFlag(true);
            listVO.add(commentsVO);
        }
        listVO.forEach(System.out::println);
        //设置当前页显示的数据集合
        pb.setList(listVO);
        //设置总页数
        int totalPage=totalCount%num==0?(totalCount/num):(totalCount/num)+1;
        pb.setTotalPage(totalPage);
        return pb;
    }


    @Override
    public PageBean<String> userNamePageQuery(int aid, int currentPage, int num, List<Integer> userIdList) {
        PageBean<String> pb=new PageBean<>();
        //设置当前页码
        pb.setCurrentPage(currentPage);
        //设置每页显示条数
        pb.setRows(num);
        //查询总记录数
        int totalCount=commentDao.findTotalCount(aid);
        //设置总记录数
        pb.setTotalCount(totalCount);
        //根据aid，start，num查询当前页的数据集合
        int start=(currentPage-1)*num; //开始的记录数
        List<String> list=new ArrayList<>();
        for (Integer i:userIdList) {
            list.add(commentDao.getAUserName(i));
        }
        //System.out.println(list+"!!!!!");
        //设置当前页显示的数据集合
        List<String> listVO=list;
        //System.out.println(listVO);
        pb.setList(listVO);
        //设置总页数
        int totalPage=totalCount%num==0?(totalCount/num):(totalCount/num)+1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public boolean getLikeComment(Integer uid, Integer com_id) {
        Integer comCount=commentDao.findByUid(uid,com_id);
        if(comCount>0) {
            return false;
        }else return true;
    }
    @Override
    public void removeLikeComment(Integer uid, Integer com_id) {
        commentDao.removeLikeComment(uid,com_id);
    }
    @Override
    public void addLikeComment(Integer uid, Integer com_id) {
        commentDao.addLikeComment(uid,com_id);
    }
}
